home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1135 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: lrz-muenchen.de!sun1!ua302aa
  2. From: ua302aa@sun1.lrz-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to dynamically allocate first element of linked list.
  5. Date: 11 Jan 1996 13:19:58 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4d32pu$e8g@sparcserver.lrz-muenchen.de>
  9. References: <erkDL007G.rt@netcom.com>
  10. NNTP-Posting-Host: sun1.lrz-muenchen.de
  11.  
  12. erk@netcom.com (Staugher) writes:
  13.  
  14. >Im trying (without any luck )to write a function to allocate the first element 
  15. >of a doubly linked list in C++. Unfortunately I learned data structures in 
  16. >Pascal and have to port my knowledge (or lack of ) to C.
  17.  
  18. >Here is my arrangement:
  19.  
  20.  
  21. >struct elmnt
  22. >    { char name[20];
  23. >      char number[17];
  24. >      struct elmnt *forward_pointer;
  25. >      struct elmnt *reverse_pointer;
  26. >    }
  27.  
  28. This is the first obvious problem with your code. Not reminating the 
  29. definition of "struct emnt" with a semicolon defines main to return 
  30. a "struct elmt". 
  31.  
  32. Avoiding implicit "int" functions helps to protect you from this 
  33. type of error.
  34.  
  35. >//======================================
  36.  
  37. This is the second obvious problem. In C, "//" is a syntax error,
  38. not a comment delimiter.
  39.  
  40. >main()
  41. >{
  42. >struct elmnt *list = newlist(void)
  43.  
  44. This is the third obvious problem. You cannot use "void" the way you
  45. do. Change this to
  46.  
  47.    struct elmnt *list = newlist();
  48.  
  49. >}
  50.  
  51. >//======================================
  52.  
  53. >struct elmnt * newlist(void)
  54. >{
  55. >struct elmnt *newptr;
  56. >newptr = malloc(sizeof(struct elmnt));
  57.  
  58. For all we know, malloc() is an external function returning int and
  59. taking one argument of type size_t. While this second assuption
  60. is correct, the first one is wrong and can lead to unexpected results
  61. in some environments.
  62.  
  63. >return (newptr);
  64. >}
  65.  
  66. >//=======================================
  67.  
  68.  
  69. >The compiler complains with an ERROR that it cannot convert
  70. >(void *) to (elmnt *) in function newlist
  71.  
  72. If you compile this code and the only diagnostic is that a "void *"
  73. cannot be converted to a "elmnt *", talk to your compiler vendor.
  74.  
  75. In the posted piece of code, there is no type "elmnt", so it is
  76. pretty obvious that there cannot be a pointer to that type.
  77.  
  78. Kurt
  79.  
  80. -- 
  81. | Kurt Watzka                             Phone : +49-89-2180-6254
  82. | watzka@stat.uni-muenchen.de
  83. | ua302aa@sunmail.lrz-muenchen.de
  84.